﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using MegavaniaX.Messaging.Events;
namespace MegavaniaX.Sound
{
    internal static class SoundManager
    {
        private static IList<SoundEffect> soundEffects = new List<SoundEffect>();
        private static IList<SoundEffectInstance> soundEffectInstances = new List<SoundEffectInstance>();
        private static ContentManager content = null;


        public static void Init(ContentManager c)
        {
            content = c;
        }

        public static void PlaySound (string soundEffectName)
        {
            if (content == null)
                throw new MegavaniaException("Content Manager not initialized.");

            if (!soundEffects.Any(se => se.Name == soundEffectName))
            {
                MegavaniaX.logger.DebugFormat("Sound effect {0} not found, adding to list", soundEffectName);
                var se = content.Load<SoundEffect>(soundEffectName);
                se.Name = soundEffectName;
                soundEffects.Add(se);
            }
            var effect = (soundEffects.FirstOrDefault(se => se.Name == soundEffectName));
            if (effect == null)
            {
                MegavaniaX.logger.ErrorFormat("Could not find sound effect with name {0}!", soundEffectName);
            }
            else
            {
                var effectInstance = effect.CreateInstance();
                effectInstance.IsLooped = false;
                effectInstance.Play();
            }
        }

        private static void PlaySEI (SoundEffectInstance instance)
        {
            // first clean up old SEI
            var sesToDispose = soundEffectInstances.Where(se => se.State == SoundState.Stopped).ToList();
            foreach (var se in sesToDispose)
            {
                if (!se.IsDisposed)
                {
                    se.Dispose();
                }
                soundEffectInstances.Remove(se);
            }

            if (soundEffectInstances.Count >= 16) // 16 is the max number of SEI that can play as once
            {
                soundEffectInstances.RemoveAt(0); // remove oldest
            }
            soundEffectInstances.Add(instance);
            instance.Play();

        }


        public static void DisposeAll()
        {
            foreach (var sei in soundEffectInstances)
            {
                if (!sei.IsDisposed)
                {
                    sei.Dispose();
                }
            }

            foreach (var se in soundEffects)
            {
                if (!se.IsDisposed)
                {
                    se.Dispose();
                }
            }

        }

        public static void PlaySoundEvent(IMegavaniaEvent evt)
        {
            var soundEvent = (PlaySoundEvent)evt;

            PlaySound(soundEvent.SoundName);
        }


    }
}
